package com.example.demo.trace;

import com.example.demo.Span;
import com.example.demo.SpanManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyTraceAspect {
    //@Around(value = "@within(org.springframework.stereotype.Controller) || @within(org.springframework.stereotype.Service)")
    @Around(value = "execution(* com.example..*.*(..))")
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {

        Span start = SpanManager.start(proceedingJoinPoint.getTarget().getClass().getName() + "." + proceedingJoinPoint.getSignature().getName());

        try {
            Object obj = proceedingJoinPoint.proceed();
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            SpanManager.stop(start);
        }
        return null;
    }


}
